<?php

require 'facebook.php';
require 'config.inc.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $fbconfig['app_id'],
  'secret' => $fbconfig['secret_key'],
  'cookie' => true,
));

$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
  } catch (FacebookApiException $e) {
    echo "Somethings wrong";
  }
}

$con = mysql_connect("localhost", "vatsav", "2T*vzcd");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("vatsav", $con);
if (!$db_selected) {
	die ('Cant use db : ' . mysql_error());
}

$uid = $_POST["uid"];
$offset = $_POST["offset"];
$tag = $_POST["tag"];
$tag = stripslashes($tag);
$tag = htmlentities($tag,ENT_QUOTES,'UTF-8');
$limit = $_POST["limit"];
$limit++;
//need to use table joins

$query = "select * from links where uid=$uid and linkid in (select linkid from taglink where uid=$uid and tagid in (select tagid from tags where uid=$uid and tag='$tag')) LIMIT $offset,$limit";
$limit--;
$result = mysql_query($query);	
// Check result
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
   echo $message;
}
$num_rows = mysql_num_rows($result);
$i=0;

while($row = mysql_fetch_array($result))
{
	$i++;
	if($i<=$limit)
	{
		//uid of the user who shared the link
		$id = $row[3]; //uid
		$linkid = $row[0];
		//no. of likes
		$query = "select distinct count(*) from likes where uid=".$id." and linkid=".$linkid;
		$likes_result = mysql_query($query);	
		if (!$likes_result) {
		   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
		   echo $message;
		}
		$likes_row = mysql_fetch_row($likes_result);
		$num_likes=$likes_row[0];
		$link_url = $row[1];
		$ts = $row[2];
		$groupid = $row[4];
		$commentid = $row[5];
		$link_desc = $row[6];
		$link_desc_len = strlen($link_desc);	
			$link_desc = substr($link_desc,0,200);
		if($link_desc_len != strlen($link_desc))
		{
			$link_desc .= "...";
		}
		$link_title = $row[7];
		$link_pic= $row[8];
		if(is_null($link_pic))
			$link_pic = "images/no_image.gif";

		$user = $facebook->api('/'.$id);
		$name = $user["name"];
		
		$query = "select group_name from groups where uid=".$id." and groupid=".$groupid;
		$result2 = mysql_query($query);	
		if (!$result2) {
		   $message  = 'Invalid query: ' . mysql_error() . "<br/>";
		   echo $message;
		}
		$row2 = mysql_fetch_row($result2);
		$group_name=$row2[0];
		$group_name_len = strlen($group_name);	
		$group_name_new = substr($group_name,0,30);
		if($group_name_len != strlen($group_name))
		{
			$group_name_new .= "...";
}
	
?>
<div class="post">
	<div class="post_left">
		<img class="img_src" alt="No Image Available" src="<?php echo $link_pic; ?>" />
		<div class="like">
			<a class="likes_url" onclick="javascript:like(<?php echo $id.','.$linkid; ?>)"> 
				<img src="images/thumbs-up-small.png" alt="like"/>  <?php echo $num_likes; ?> 
			</a>
		</div>
	</div>

	<div class="post_right">
		<div class="post_url">  <a target="_blank" class="post_title" href="<?php echo $link_url; ?>"> <?php echo $link_title; ?> </a> 	</div>
		<div class="post_desc"> <?php echo $link_desc; ?> 	</div>
		
		<div class="post_footer"> By 
		  	<a class="small_link" onclick="javascript:friendProfile(<?php echo $id;?>)"><?php echo $name; ?></a> in <a class="small_link" onclick="javascript:group(<?php echo $uid;?>,<?php echo $groupid; ?>,'<?php echo $group_name;?>',0,4)"><?php echo $group_name_new;?></a>. 
			<a class="small_link" onclick="javascript:singleLink(<?php echo $id.','.$linkid; ?>)">comments</a>
		</div>
	</div>
</div>

<?php
	}
}

?>

<div class="button">
<?php 
	$prev = $offset-$limit;
	$next = $offset+$limit;
	if($offset!=0)
	{
		echo "<a class='simple_button' onclick='javascript:singleTag(\"$tag\",$prev,$limit)'>Previous</a> ";
	}
	
	if($limit < $num_rows){
		echo "<a class='simple_button' onclick='javascript:singleTag(\"$tag\",$next,$limit)'>Next</a>";
	}
	
mysql_close($con);

?>
</div>
